const isGetCode = false;

const onTouch = false;

const reg = /^[A-Za-z0-9]{6,20}$/;

const phoneReg = /^(\+?81|0)\d{1,4}[ \-]?\d{1,4}[ \-]?\d{4}$/;

var downloadObj = {
    'ios' : 'https://cfsdw.tf-ff.xyz/#/join/OfRphw',
    'android' : 'https://admin.slot365.live/storage/files/pachinko.apk'
}


function download (version) {
    const from = $('#pid').val();
    if (from === '') {
        window.location.href = this.downloadObj[version];
    } else {
        alert('登録後ダウンロードしてください')
        window.location.href = '#fh5co-pricing'
    }
}
function sign () {
    const params = {}
    params.phone = $('#phone').val();
    params.username = $('#username').val();
    params.code = $('#code').val();
    params.password = $('#password').val();
    params.pid = $('#pid').val();
    params.password_agin = $('#password_agin').val();
    const rules = {
        username: () => params.username !== '' && params.username.length < 10 && typeof params.username === 'string',
        phone: () => Number.isInteger(parseInt(params.phone)) && phoneReg.test(params.phone),
        password : () => reg.test(params.password) && params.password === params.password_agin,
        code : () => Number.isInteger(parseInt(params.code)) && params.code !== '' && params.code.length < 5,
    };
    const message = {
        username : 'ユーザー名を空白にすることはできません',
        phone : '電話番号は数字で１２桁未満になります',
        code : '認証番号は数字で５桁未満になります',
        password : 'パスワードは6から20文字の数字または英字。',
    }
    const result = validateRequestParams(params,rules,message)
    if (result === true) {
        if (this.onTouch === true) {
            console.log('reTouch')
            return;
        }
        this.onTouch = true;
        const url = 'https://admin.slot365.live/api/register';
        var that = this;
        request(url,'POST',params,function (res) {
            if (res.status === 200) {
                var hostname = window.location.href;
                var address = hostname.split("?")[0];
                alert('新規登録完了しました、ログインしてください');
                window.location.href = address;
            } else {
                alert(res.msg)
            }
            that.onTouch = false;
        })
    }
}


function openToLine()
{
    if (/Mobi|Android|iPhone/i.test(navigator.userAgent)) {
        // 如果是手机设备，跳转到 Line 应用
        window.location.href = "line://ti/p/@941vwwhi";
    } else {
        // 如果是PC设备，跳转到 Line 网页
        window.location.href = "https://line.me/R/ti/p/@941vwwhi";
    }
}
function validateRequestParams(params, validationRules, errorMessages) {
    if (typeof params !== 'object') {
        throw new Error('参数必须是一个对象');
    }

    if (!validationRules || typeof validationRules !== 'object') {
        throw new Error('缺少验证规则或验证规则必须是一个对象');
    }
    for (let key in validationRules) {
        if (validationRules.hasOwnProperty(key)) {
            if (!params.hasOwnProperty(key)) {
                throw new Error(`参数缺失必要字段: ${key}`);
            }
            const validationFunction = validationRules[key];
            const validationResult = validationFunction(params[key]);
            if (!validationResult) {
                const errorMessage = errorMessages && errorMessages[key] ? errorMessages[key] : `参数验证失败: ${key}`;
                // throw new Error(errorMessage);
                alert(errorMessage);
                return false
            }
        }
    }
    return true;
}
function sendCode () {
    const phone = $('#phone').val();
    if (!phoneReg.test(phone)) {
        alert('正しい電話番号を入力してください');
        return false
    }
    if (this.isGetCode === true) {
        alert('繰り返さないでください')
        return false
    }
    this.isGetCode = true;
    const url = 'https://admin.slot365.live/api/registerGetCode'
    const params = {phone_number:phone};
    var that = this;
    request(url,'GET',params,function (res) {
        if (res.status === 200) {
            alert('確認コードが送信されましたので、確認してください');
            $('#codeBtn').html('確認してください')
        } else {
            alert(res.msg)
        }
        that.isGetCode = false;
    })
}
function request (url,type,data,callBack) {
    $.ajax({
        url: url, // 替换为你的API端点
        type: type, // 请求类型，可以是GET、POST等
        data:data,
        dataType: 'json', // 期望从服务器返回的数据类型
        success: function(response) {
            // 请求成功时的回调函数
            callBack(response)
        },
        error: function(xhr, status, error) {
            // 请求失败时的回调函数
            console.error('Error:', error);
        }
    });
}
function getQueryParams() {
    var search = window.location.search;
    var regex = /[?&]([^=#]+)=([^&#]*)/g;
    var params = {};
    var match;
    while (match = regex.exec(search)) {
        params[match[1]] = decodeURIComponent(match[2].replace(/\+/g, ' '));
    }
    return params;
}
$(function(){
    const params = getQueryParams()
    if (params.pid) {
        $('#pid').val(params.pid);
    } else {
        $('#pid').val('');
    }
    $('#colour-variations ul').styleSwitcher({
        defaultThemeId: 'theme-switch',
        hasPreview: false,
        cookie: {
            expires: 30,
            isManagingLoad: true
        }
    });
    $('.option-toggle').click(function() {
        $('#colour-variations').toggleClass('sleep');
    });
});